package org.achievement.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.achievement.system.common.RequestResult;
import org.achievement.system.dao.CourseDao;
import org.achievement.system.pojo.entity.Course;
import org.achievement.system.pojo.form.CourseForm;
import org.achievement.system.service.CourseService;

import org.achievement.system.pojo.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements CourseService {

    @Autowired
    private CourseDao courseDao;

    @Override
    public RequestResult list(CourseForm courseForm) {
        if (courseForm != null) {
            if (courseForm.getPageSize() == null || courseForm.getPageSize() <= 0) {
                courseForm.setPageSize(10);
            }
            int offset = (courseForm.getPage() - 1) * courseForm.getPageSize();
            courseForm.setOffset(offset);
        }
        List<CourseVo> list = courseDao.list(courseForm);
        Integer totalSize = courseDao.count(courseForm);
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("list", list);
        resultData.put("totalSize", totalSize);
        return RequestResult.success(resultData);
    }
}